package com.itheima.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.ObjectId;
import com.itheima.mongoUtil.MovementTimeLineService;
import com.itheima.tanhua.api.mongo.MovementApi;
import com.itheima.tanhua.enums.CommentType;
import com.itheima.tanhua.pojo.mongo.Comment;
import com.itheima.tanhua.pojo.mongo.Friend;
import com.itheima.tanhua.pojo.mongo.Movement;
import com.itheima.tanhua.pojo.mongo.MovementTimeLine;
import com.itheima.tanhua.until.mongo.IdWorker;
import com.itheima.tanhua.vo.mongo.pageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 芝士雪豹
 * @create 2022/9/24/18:50
 */
@DubboService
public class MovementApiImp implements MovementApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private MovementTimeLineService movementTimeLineService;

    @Override
    public void publish(Movement movement) {
        try {
            //保存动态状态
            //设置PID
            movement.setPid(idWorker.getNextId("movement"));
            //设置时间
            movement.setCreated(System.currentTimeMillis());
            //保存数据
            mongoTemplate.save(movement);
            ////查询当前用户的好友数据
            //Query query=Query.query(Criteria.where("id").is(movement.getTextContent()));
            //List<Friend> friends = mongoTemplate.find(query, Friend.class);
            ////循环好友数据,构建时间数据存入数据库
            //for (Friend friend : friends) {
            //    MovementTimeLine timeLine = new MovementTimeLine();
            //    timeLine.setCreated(System.currentTimeMillis());
            //    timeLine.setMovementId(friend.getId());
            //    timeLine.setFriendId(friend.getFriendId());
            //    timeLine.setUserId(friend.getUserId());
            //    mongoTemplate.save(timeLine);
            //}

            movementTimeLineService.movementTimeLineService(movement.getUserId(), movement.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public pageResult findByuserId(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId))
                .skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Movement> movements = mongoTemplate.find(query, Movement.class);
        return new pageResult(page, pagesize, movements);
    }

    @Override  //userId=firendId
    public List<Movement> findFriend(Long userId, Integer page, Integer pagesize) {
        //根据创建时间倒序排 利用 传入的id 进行分页查询
        Query query = Query.query(Criteria.where("friendId").is(userId))
                .skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        //提取动态id
        List<ObjectId> movementId = CollUtil.getFieldValues(list, "movementId", ObjectId.class);
        //使用动态id 去mongo数据库
        Query query1 = Query.query(Criteria.where("id").in(movementId));
        //返回查找的 用户数据
        List<Movement> movementList = mongoTemplate.find(query1, Movement.class);
        return movementList;
    }

    /**
     * 随机找10个
     *
     * @param pagesize
     * @return
     */
    @Override
    public List findRandomten(Integer pagesize) {
        //创建统计对象,设置统计参数
        TypedAggregation aggregation = Aggregation.newAggregation(Movement.class, Aggregation.sample(pagesize));
        //调用 mongoTemplate 方法统计
        AggregationResults<Movement> aggregate = mongoTemplate.aggregate(aggregation, Movement.class);
        //获取统计结果
        return aggregate.getMappedResults();
    }

    /**
     * 根据 查到的一批 pid进行查找
     *
     * @param pid
     * @return
     */
    @Override
    public List findforPid(List<Long> pid) {
        Query query = Query.query(Criteria.where("pid").is(pid));
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        return list;
    }

    //根据id查询数据
    @Override
    public Movement findById(String movementId) {
        //Query query=Query.query(Criteria.where("movementId").is(movementId));
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        return movement;
    }




}
